<?php

class DefaultController extends Controller
{
	public function actionIndex()
	{
		$value = $_POST['value'];
		$id = $_POST['id'];
		
		$id = str_replace('sitesnippet_', '', $id);
		
		$model = SiteSnippet::model()->findByPk($id);
		
		if($model == null)
		{
			throw new CHttpException(404, 'No such snippet!');
		}
		else if(!$this->module->canEdit())
		{
			$this->renderPartial('index', array('model' => $model), false, true);
			return;
		}
		
		$model->text = Yii::app()->format->html($value);
		if($model->save())
		{
			$cache = Yii::app()->{Yii::app()->getModule('sitesnippet')->cacheId};
			$cache->delete('sitesnippet_'.$id);
		}
		
		$this->renderPartial('index', array('model' => $model), false, true);
	}
}